﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ivory;

namespace Ivory.GUI
{
    public class Text : GUIComponent
    {
        //Fields
        private string _FontLocation;

		private string _Txt; 
		public string Txt
		{
			get { return _Txt; }
            set { _Txt = value; ResizeComponent(); }
		} 
 
		private SpriteFont _TextFont; 
		public SpriteFont TextFont
		{
			get { return _TextFont; }
            set { _TextFont = value; ResizeComponent(); }
		}

        private Vector2 _Location = Vector2.Zero;
        public Vector2 Location
        {
            get { return _Location; }
            set { _Location = value; }
        } 
 
		private Vector2 _TextOrigin = Vector2.Zero; 
		public Vector2 TextOrigin
		{
			get { return _TextOrigin; }
			set { _TextOrigin = value; }
		} 
 
		private Color _TextColor = Color.White; 
		public Color TextColor
		{
			get { return _TextColor; }
			set { _TextColor = value; }
		} 		
        
        //Construct
        public Text(Game game, SpriteFont font, string text, Vector2 location)
            : base(game, new Rectangle((int)location.X, (int)location.Y, (int)font.MeasureString(text).X, (int)font.MeasureString(text).Y))
        {
            this.Location = location;
            this.Txt = text;
            this.TextFont = font;
        }

        public Text(Game game, string fontLocation, string text, Vector2 location)
            : base(game, new Rectangle((int)location.X, (int)location.Y, 0, 0))
        {
            this.Location = location;
            this.Txt = text;
            this._FontLocation = fontLocation;
        }
        
        //Methods
        protected override void ResizeComponent()
        {
            if (TextFont == null || string.IsNullOrEmpty(Txt))
                return;

            this.ClickRectangle = new Rectangle((int)Location.X, (int)Location.Y, (int)TextFont.MeasureString(Txt).X, (int)TextFont.MeasureString(Txt).Y);

        }

        public override void DrawComponent(SpriteBatch batch, GameTime time)
        {
            batch.DrawString(TextFont, Txt, Location, TextColor);
        }

        public override void LoadComponent()
        {
            if (string.IsNullOrEmpty(_FontLocation))
            {
                if (TextFont == null)
                    throw new IvoryFrameworkError(Errors.AssetLoadingException);
            }
            else
                TextFont = Game.Content.Load<SpriteFont>(_FontLocation);

            ResizeComponent();
        }
    }
}
